package com.yd.controller.app;


import java.io.IOException;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yd.controller.yd.YD_Users;
import com.yd.pojo.Balance;
import com.yd.pojo.MoneyRecord;
import com.yd.service.BalanceService;
import com.yd.service.MoneyRecordService;
import com.yd.tool.UtsBigdeciArith;
import com.yd.tool.UtsReg;


@Controller
public class MoneyRecordControllerApp {

	@Resource
	private MoneyRecordService moneyRecordService;
	@Resource
	private BalanceService balanceService;
	
	//商戶看到的结款记录表list
	@RequestMapping("/app/kefu/MoneyRecord/list")
	@ResponseBody
	public String app_kefu_MoneyRecord_list(HttpServletRequest request) throws MalformedURLException, IOException {
		JSONObject jsonObject = new JSONObject();
		
		String token="";
		String loginname = "";
		String page = "";
		String size = "";
		
		if (UtsReg.getStringKB(request.getParameter("token"))) {token = request.getParameter("token").trim();}
		if (UtsReg.getStringKB(request.getParameter("loginname"))) {loginname = request.getParameter("loginname").trim();}
		if (UtsReg.getIntXl(request.getParameter("page"))) {page = request.getParameter("page").trim();}{page = "1";}
		if (UtsReg.getIntXl(request.getParameter("size"))) {size = request.getParameter("size").trim();}{size = "10";}
		//验证token
		JSONObject yd_users = YD_Users.getUsersByToken(token);
		if (yd_users.getString("error").equals("1")) {jsonObject.put("msg", yd_users.getString("msg"));jsonObject.put("error", "1");return jsonObject.toString();}
		JSONObject users_token = JSONObject.parseObject(yd_users.getString("data"));

		PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(size));
        List<MoneyRecord> list = null;
		EntityWrapper<MoneyRecord> ew = new EntityWrapper<MoneyRecord>();
		if (loginname.length() > 0) {ew.eq("loginname", loginname);}
		list = moneyRecordService.selectList(ew.orderBy("id", false));
        PageInfo<MoneyRecord> pages = new PageInfo<MoneyRecord>(list);

		jsonObject.put("data", pages);
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();
	}
	//商户自己结款记录表list
	@RequestMapping("/app/merchant/MoneyRecord/list")
	@ResponseBody
	public String app_merchant_MoneyRecord_list(HttpServletRequest request) throws MalformedURLException, IOException {
		JSONObject jsonObject = new JSONObject();
		String token="";
		String page = "";
		String size = "";
		if(UtsReg.getStringKB(request.getParameter("token"))){token=request.getParameter("token").trim();}else{jsonObject.put("msg", "token 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if(UtsReg.getIntXl(request.getParameter("page"))) {page = request.getParameter("page").trim();}{page = "1";}
		if(UtsReg.getIntXl(request.getParameter("size"))) {size = request.getParameter("size").trim();}{size = "10";}
		
		//验证token
		JSONObject yd_users = YD_Users.getUsersByToken(token);
		if (yd_users.getString("error").equals("1")) {jsonObject.put("msg", yd_users.getString("msg"));jsonObject.put("error", "1");return jsonObject.toString();}
		JSONObject users_token = JSONObject.parseObject(yd_users.getString("data"));
		
		PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(size));
        List<MoneyRecord> list = null;
		EntityWrapper<MoneyRecord> ew = new EntityWrapper<MoneyRecord>();
		list = moneyRecordService.selectList(ew.eq("usersid", users_token.getString("id")));
        PageInfo<MoneyRecord> pages = new PageInfo<MoneyRecord>(list);

		jsonObject.put("data", pages);
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();
	}
	
	//商户提款申请
		@RequestMapping("/app/MoneyRecord/shenqing/add")
		@ResponseBody
		public String app_MoneyRecord_shengqing_add(HttpServletRequest request) throws MalformedURLException, IOException {
			JSONObject jsonObject = new JSONObject();

			String token="";
			String price="";
			String shoukuanzhanghao="";
			String shoukuanzhanghaohuzhu="";
			String shoukuanyinhangaddress="";
			
		
			if(UtsReg.getStringKB(request.getParameter("token"))){token=request.getParameter("token").trim();}else{jsonObject.put("msg", "token 错误");jsonObject.put("error", "1");return jsonObject.toString();}
			if(UtsReg.getIntXl(request.getParameter("price"))){price=request.getParameter("price").trim();}else{jsonObject.put("msg", "金额 错误");jsonObject.put("error", "1");return jsonObject.toString();}
			if(UtsReg.getStringKB(request.getParameter("shoukuanzhanghao"))){shoukuanzhanghao=request.getParameter("shoukuanzhanghao").trim();}else{jsonObject.put("msg", "收款账号 错误");jsonObject.put("error", "1");return jsonObject.toString();}
			if(UtsReg.getStringKB(request.getParameter("shoukuanzhanghaohuzhu"))){shoukuanzhanghaohuzhu=request.getParameter("shoukuanzhanghaohuzhu").trim();}else{jsonObject.put("msg", "收款账号户主 错误");jsonObject.put("error", "1");return jsonObject.toString();}
			if(UtsReg.getStringKB(request.getParameter("shoukuanyinhangaddress"))){shoukuanyinhangaddress=request.getParameter("shoukuanyinhangaddress").trim();}else{jsonObject.put("msg", "收款银行卡地址 错误");jsonObject.put("error", "1");return jsonObject.toString();}
			//验证token
			JSONObject yd_users = YD_Users.getUsersByToken(token);
			if (yd_users.getString("error").equals("1")) {jsonObject.put("msg", yd_users.getString("msg"));jsonObject.put("error", "1");return jsonObject.toString();}
			JSONObject users_token = JSONObject.parseObject(yd_users.getString("data"));
			System.out.println(users_token);
			EntityWrapper<Balance> ew=new EntityWrapper<Balance>();
			Balance balance=balanceService.selectOne(ew.eq("usersid",users_token.getString("id")));
			if(balance==null){
				jsonObject.put("msg", "没有此数据");jsonObject.put("error", "1");return jsonObject.toString();
			}
			
			if(Double.valueOf(balance.getShijiyingjiekuan())<Double.valueOf(price)|Double.valueOf(balance.getShijiyingjiekuan())==null){
				jsonObject.put("msg", "可提现余额不够！");
				jsonObject.put("error", "1");
				return jsonObject.toString();
				
			}
			MoneyRecord MoneyRecord=new MoneyRecord();
			MoneyRecord.setUsersid(users_token.getString("id"));
			MoneyRecord.setUsersname(users_token.getString("name"));
			MoneyRecord.setUsersimg(users_token.getString("img"));
			MoneyRecord.setPrice(price);
			MoneyRecord.setShoukuanzhanghao(shoukuanzhanghao);
			MoneyRecord.setShoukuanzhanghaohuzhu(shoukuanzhanghaohuzhu);
			MoneyRecord.setShoukuanyinhangaddress(shoukuanyinhangaddress);
			MoneyRecord.setState("0");
			if(!moneyRecordService.insert(MoneyRecord)){
				jsonObject.put("msg", "失败");jsonObject.put("error", "1");return jsonObject.toString();
			}
			balance.setYingjiekuan(UtsBigdeciArith.sub(new BigDecimal(balance.getYingjiekuan()), new BigDecimal(price))+"");
			balance.setShijiyingjiekuan(UtsBigdeciArith.sub(new BigDecimal(balance.getShijiyingjiekuan()), new BigDecimal(price))+"");
			balance.setYijiekuan(UtsBigdeciArith.add(new BigDecimal(balance.getYijiekuan()), new BigDecimal(price))+"");
			
			if(!balanceService.updateById(balance)){
				jsonObject.put("msg", "失败");jsonObject.put("error", "1");return jsonObject.toString();
			}
			
			
			jsonObject.put("msg", "成功");
			jsonObject.put("error", "0");
			return jsonObject.toString();
		}
	
	
	
	
	

}
